package com.hh.eduservice.controller;


import com.hh.commonutils.Result;
import com.hh.eduservice.entity.EduVideo;
import com.hh.eduservice.service.EduVideoService;
import com.hh.eduservice.service.client.VideoIntervice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author huhuan
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VideoIntervice videoIntervice;

    @PostMapping("/addVideo")
    public Result addChapter(@RequestBody EduVideo eduVideo){
        eduVideoService.addVideo(eduVideo);
        return Result.success();
    }

    @DeleteMapping("deleteVideoById/{videoId}")
    public Result deleteVideoById(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getEduVideoById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!videoSourceId.isEmpty()) {
            Result result = videoIntervice.deleteUploadVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new RuntimeException("可能生产者挂掉了，看熔断器消息："+result.getData().get("msg"));
            }
        }
        eduVideoService.removeById(videoId);
        return Result.success();
    }

    @PostMapping("updateVideoById")
    public Result updateVideoById(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return Result.success();
    }

    @GetMapping("/getEduVideoById/{videoId}")
    public Result getEduVideoById(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getEduVideoById(videoId);
        return Result.success().data("eduVideo",eduVideo);
    }
}

